<template>
	<view class="">
		<view v-if="couponList.length > 0">
			<view v-for="(item, index) in couponList" :key="item.businessId">
				<coupon-cell 
				:businessId="item.businessId"
				:discountAmount="item.money"
				:rangInfo="item.conditionMoney"
				:activityType="item.couponName"
				:durationDate="item.useTime"
				:couponType="item.businessName"
				:storeRang = "item.applicableUnit"
				:isReceive="item.isReceive"
				:useNotice="item.useNotice"
				:useArea = "item.useArea"
				:businessType="0"
				:useTypeName="item.useTypeName"
				:applicableUnitType="item.applicableUnitType"
				@getCoupon="getCoupon"
				></coupon-cell>
			</view>
			<u-loadmore :status="loadingStatus" @loadmore="clickloadmore"></u-loadmore>
		</view>
		
		<u-empty v-if="couponList.length == 0" mode="coupon" marginTop="160"></u-empty>
		<login-mask></login-mask>
	</view>
</template>

<script>
	import couponCell from '../../components/coupon/coupon-cell.vue'
	import loginMask from '@/components/layout/login-mask.vue'
	import {apiCouponList,receiveCoupons} from '../../common/api.js'
	export default {
			
		components: {
			couponCell,
			loginMask
		},
		data() {
			return {
				pageNo: 1,
				loadingStatus: 'loading',
				couponList: []
			}
		},
		onLoad() {
			this.getData();
		},
		methods: {
			getData() {
				apiCouponList({
					pageNo : this.pageNo,
					tenantId : this.$store.state.user.tenantCode,
					applicableUnitNo : this.$store.state.user.deptId,
					memberNo : this.$store.state.user.userInfo.customerId
				}).then(res => {
					
					if (this.pageNo == 1) {
						this.couponList = [];
						this.pageNo++;
						// uni.stopPullDownRefresh();
					
						this.loadingStatus = 'loadmore';
					} else {
						if (!uni.$u.test.isEmpty(res)) {
							this.pageNo++;
					
							this.loadingStatus = 'loadmore';
						} else {
					
							this.loadingStatus = 'nomore';
						}
					}
					
					if (!uni.$u.test.isEmpty(res)) {
						this.couponList = this.couponList.concat(res);
					}
					
				})
			},
			getCoupon(data){
				if(uni.$u.test.isEmpty(this.$store.state.user.userInfo)){
					this.$store.dispatch('user/MmvLogin');
					return
				}
				
				receiveCoupons({
					memberNo : this.$store.state.user.userInfo.customerId,
					businessId : data.businessId,
				}).then(res => {
					this.pageNo = 1;
					this.getData();
					uni.$emit('refreshCouponData');
				})
			},
			
			clickloadmore() {
				this.getData();
				this.showloading = true;
				this.loadingStatus = 'loading';
			}
		},
		
		onPullDownRefresh() {
			this.pageNo = 1;
			this.getData();
		},
		
		onReachBottom() {
			this.getData();
			this.showloading = true;
			this.loadingStatus = 'loading';
		}
	}
</script>

<style lang="scss">

</style>
